//
//  SearchResultView.m
//  OrderSeat
//
//  Created by 王威 on 13-12-13.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "SearchResultView.h"
#import "ShopInfoV2Cell.h"
#import "SVProgressHUD.h"

#define TEXT_FONT   [UIFont boldSystemFontOfSize:18]
#define TEXT_FORMAT @"没有查询到任何与关键词【%@】相匹配的餐厅，请重新查询"

@implementation SearchResultView

- (id)initWithKeyWord:(NSString*)keyWord andDelegate:(id<SearchResultViewDelegate>)delegate
{
    CGRect frame = CGRectMake(10, -200, 300, 0);
    self = [super initWithFrame:CGRectMake(Zero, Zero, ScreenWidth, ScreenHeight)];
    if (self) {
        self.keyWord = keyWord;
        self.windowLevel = UIWindowLevelAlert;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        
        _m_delegate = delegate;
        _dataSource = [[SearchResultDataSource alloc] initWithView:self];
        
        _baseView = [[UIView alloc] initWithFrame:self.bounds];
        [_baseView setAlpha:0.0f];
        [self addSubview:_baseView];
        
        _backgroundView = [[UIView alloc] initWithFrame:frame];
        [_backgroundView drawBorder];
        [self addSubview:_backgroundView];
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setBackgroundNormal:@"close.png"];
        _closeBtn.frame = CGRectMake(_backgroundView.right-20, _backgroundView.top-6, 26, 26);
        [_closeBtn addTarget:self action:@selector(closeWindow) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_closeBtn];
        
        _backImageView = [[UIImageView alloc] initWithFrame:_backgroundView.bounds];
        _backImageView.image = [UIImage imageNamed:@"confirm_bg.png"];
        [_backImageView setUserInteractionEnabled:YES];
        [_backgroundView addSubview:_backImageView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = TEXT_FONT;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.backgroundColor = [UIColor blackColor];
        _titleLabel.text = @"请选择您所在的餐厅";
        [_backgroundView addSubview:_titleLabel];
        
        _shopTable = [[UITableView alloc] init];
        _shopTable.frame = CGRectMake(0, _titleLabel.bottom_5, 300, 0);
        _shopTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _shopTable.delegate = _dataSource;
        _shopTable.dataSource = _dataSource;
        _shopTable.scrollEnabled = YES;
        [_backImageView addSubview:_shopTable];

        [_dataSource searchWithKeyWord:keyWord];
        
        [SVProgressHUD showWithStatus:@"正在检索餐厅..."];
    }
    return self;
}

- (void)reLayoutSubviews
{
    if (_dataSource.shopList.count >= 3) {
        _shopTable.height = 300;
        [UIView autoHeightsNoMargin:@[_backImageView,_backgroundView]];
    }
    else if(_dataSource.shopList.count > 0){
        [UIView autoHeightsNoMargin:@[_shopTable,_backImageView,_backgroundView]];
    }
    else {
        NSString *text = [NSString stringWithFormat:TEXT_FORMAT,_keyWord];
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.bottom+30, 280, 35)];
        textLabel.font = TEXT_FONT;
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.textColor = [UIColor lightGrayColor];
        textLabel.text = text;
        [self.backImageView addSubview:textLabel];
        
        [textLabel autoHeight];
        
        _backImageView.height = textLabel.bottom + 30;
        [_backgroundView autoHeightNoMargin];
    }
    
    _backgroundView.top = 0 - _backgroundView.height;
    
    [UIView animateWithDuration:0.4 animations:^(void){
        _backgroundView.top = (ScreenHeight-_backgroundView.height)/2;
        _closeBtn.left = _backgroundView.right-20;
        _closeBtn.top = _backgroundView.top-6;
    }];
}

- (void)showWindow
{
    if ([_m_delegate respondsToSelector:@selector(willShowWindow:)]) {
        [_m_delegate willShowWindow:self];
    }
    
    [self makeKeyAndVisible];
}

- (void)closeWindow
{
    if ([_m_delegate respondsToSelector:@selector(willCloseWindow:)]) {
        [_m_delegate willCloseWindow:self];
    }

    [self makeKeyAndVisible];
    self.hidden = YES;
}

- (void)selectedRow:(NSDictionary *)row
{
    if ([_m_delegate respondsToSelector:@selector(selectedRow:)]) {
        [_m_delegate selectedRow:row];
    }
    
    [self closeWindow];
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

@implementation SearchResultDataSource

- (id)initWithView:(SearchResultView *)view
{
    self = [super init];
    if (self) {
        self.view = view;
        _shopList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)searchWithKeyWord:(NSString *)keyWord
{
    NSString *url = [NSString stringWithFormat:API_SHOP_PHOTO_SEARCH,keyWord];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    self.shopList = [data objectForKey:@"dinner"];
    [_view.shopTable reloadData];
    [_view reLayoutSubviews];
    
    [SVProgressHUD dismiss];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    [_view reLayoutSubviews];
    [SVProgressHUD dismissWithError:@"请检查网络是否通畅！" afterDelay:3];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _shopList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ShopInfoV2Cell";
    
    ShopInfoV2Cell *cell = (ShopInfoV2Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[ShopInfoV2Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    
    NSDictionary *dic = [self.shopList objectAtIndex:indexPath.row];
    [cell setContent:dic];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *row = [_shopList objectAtIndex:indexPath.row];
    [_view selectedRow:row];
}


@end

